<?php
/**
 * @Author:      zk
 * @DateTime:    18-7-24 上午10:15
 * @Description: 审批编号配置表相关
 */

class OAApprovalNumberRuleConfig extends Eloquent
{
    protected $table = 'oa_approval_number_rule_config';

    /**
     * @Author:      zk
     * @DateTime:    18-7-27 上午10:33
     * @Description: 根据审批模板id获取该审批模板的审批编号规则
     * @param int $approval_model_id 审批模板id
     * @return array
     */
    public static function getNumberRuleInfoByApprovalModelId($approval_model_id){
        $select_arr = [
            //'id', 'approval_model_id', 
            'sort', 'type', 'tag_name', 'date_type', 'add_value', 'add_places',  
        ];
        $info = OAApprovalNumberRuleConfig::where('approval_model_id', $approval_model_id)
                ->where('status', 1)
                ->select($select_arr)
                ->get()
                ->toArray();
        return $info;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-30 下午4:51
     * @Description: 根据审批模板id生成审批编号及当前增长值
     * @param int $approval_model_id 审批模板id
     * @return array
     */
    public static function getApplyNumberByApprovalModelId($approval_model_id){
        $result = [];
        $now_apply_number = OAApplyNumber::getNowApplyNumberByApprovalModelId($approval_model_id);
        if($now_apply_number){
            $now_add_value = $now_apply_number->now_add_value;
        } else {
            $now_add_value = 0;
        }
        
        $apply_number = '';
        $select_arr = [
            'oa_approval_number_rule_config.id', 'oa_approval_number_rule_config.approval_model_id', 
            'oa_approval_number_rule_config.sort', 'oa_approval_number_rule_config.type', 
            'oa_approval_number_rule_config.tag_name', 'oa_approval_number_rule_config.date_type', 
            'oa_approval_number_rule_config.add_value', 'oa_approval_number_rule_config.add_places', 
            'oa_approval_model.approval_number_separator', 
        ];
        $info = OAApprovalNumberRuleConfig::leftJoin('oa_approval_model', 'oa_approval_number_rule_config.approval_model_id', '=', 'oa_approval_model.id')
                ->where('oa_approval_number_rule_config.approval_model_id', $approval_model_id)
                ->where('oa_approval_number_rule_config.status', 1)
                ->select($select_arr)
                ->orderBy('oa_approval_number_rule_config.sort')
                ->get()
                ->toArray();
        if(empty($info)){
            return $result;
        }
        $approval_number_separator = '';    //分隔符
        //类型 (1:标签;2:日期;3:增长值;)
        foreach ($info as $k => $v) {
            $approval_number_separator = $v['approval_number_separator'];
            switch ($v['type']) {
                case 1:     //1:标签;
                    if($v['approval_number_separator'] != ''){
                        $apply_number .= $v['tag_name'].$v['approval_number_separator'];
                    } else {
                        $apply_number .= $v['tag_name'];
                    }
                    break;
                case 2:     //2:日期;
                    $apply_number .= self::getNumberByDateType($v['date_type'], $v['approval_number_separator']);
                    break;
                case 3:     //3:增长值;
                    if($now_add_value != 0){
                        $apply_number .= self::getNumberByAddValue($now_add_value, $v['add_places'], $v['approval_number_separator']);
                    } else {
                        $now_add_value = $v['add_value'] - 1;
                        $apply_number .= self::getNumberByAddValue(($v['add_value'] - 1), $v['add_places'], $v['approval_number_separator']);
                    }
                    break;
                default:
                    $apply_number .= $apply_number;
                    break;
            }
        }
        $apply_number = rtrim($apply_number, $approval_number_separator);
        $result = [
            'apply_number' => $apply_number,
            'add_value' => $now_add_value + 1,
        ];
        
        return $result;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-30 下午5:35
     * @Description: 根据日期类型、分隔符     获取组合之后的编号字符串
     * @param int $date_type    日期类型(1:YYYY;2:YYYYmm;3:YYYY-mm;4:YYYYmmdd;5:YYYY-mm-dd;)
     * @param string $separator 分隔符
     * @return string   编号字符串
     */
    public static function getNumberByDateType($date_type, $separator = ''){
        switch ($date_type) {
            case 1:
                $date = date('Y', time());
                break;
            case 2:
                $date = date('Ym', time());
                break;
            case 3:
                $date = date('Y-m', time());
                break;
            case 4:
                $date = date('Ymd', time());
                break;
            case 5:
                $date = date('Y-m-d', time());
                break;
            default:
                $date = '';
                break;
        }
        if($separator != ''){
            $number = $date.$separator;
        } else {
            $number = $date;
        }
        return $number;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-30 下午5:50
     * @Description: 根据初始增长值、位数     获取组合之后的编号字符串
     * @param int $add_value    初始增长值
     * @param int $add_places   增长值位数
     * @return string   编号字符串
     */
    public static function getNumberByAddValue($add_value, $add_places, $separator = ''){
        $add_value += 1;
        if($separator != ''){
            $number = sprintf('%0'.$add_places.'s', $add_value).$separator;
        } else {
            $number = sprintf('%0'.$add_places.'s', $add_value);
        }
        return $number;
    }
    
}
